package com.colors.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.colors.springcloud.entities.CommonResult;
import com.colors.springcloud.entities.Payment;
import com.colors.springcloud.myhandler.CustomBlockHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author colors
 * @date 2021/1/13 17:58
 */
@RestController
@Slf4j
public class RateLimitController {

    @GetMapping("/byResource")
    @SentinelResource(value = "byResource", blockHandler = "handleException")
    public CommonResult byResource() {
        return new CommonResult(200, "按资源名称限流测试ok", new Payment(2021L,"serial001"));

    }

    public CommonResult handleException(BlockException blockException) {
        return new CommonResult(400,blockException.getClass().getCanonicalName()+"\t 服务不可用");
    }

    @GetMapping("/customBlockHandler")
    @SentinelResource(value = "customBlockHandler", blockHandlerClass = CustomBlockHandler.class, blockHandler = "customHandlerException")
    public CommonResult customBlockHandler() {
        return new CommonResult(200, "客户定义限流处理", new Payment(2022L,"serial002"));
    }

}
